package com.xw.service.impl;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xw.entity.Reserve;
import com.xw.entity.ReserveItem;
import com.xw.mapper.ReserveItemMapper;
import com.xw.mapper.ReserveMapper;
import com.xw.service.IReserveItemService;
import com.xw.utils.R;
import com.xw.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2021-09-15
 */
@Service
public class ReserveItemServiceImpl extends ServiceImpl<ReserveItemMapper, ReserveItem> implements IReserveItemService {

    @Resource
    private ReserveItemMapper reserveItemMapper;

    @Resource
    private ReserveMapper reserveMapper;

    @Override
    public ResultVO<?> findReserveItemList(Page page, HttpServletRequest request) {

        String nickname = request.getParameter("nickname");
        String name = request.getParameter("name");
        String idcard = request.getParameter("idcard");
        String phone = request.getParameter("phone");
        String reserveTime = request.getParameter("reserveTime");


        IPage<ReserveItem> iPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<ReserveItem>(page.getPageNumber(), page.getPageSize());

        LambdaQueryWrapper<ReserveItem> queryWrapper = new LambdaQueryWrapper<ReserveItem>();
        queryWrapper.orderByDesc(ReserveItem::getId);

        queryWrapper.like(!ObjectUtils.isEmpty(nickname), ReserveItem::getNickname, nickname);
        queryWrapper.like(!ObjectUtils.isEmpty(name), ReserveItem::getName, name);
        queryWrapper.like(!ObjectUtils.isEmpty(idcard), ReserveItem::getIdcard, idcard);
        queryWrapper.like(!ObjectUtils.isEmpty(phone), ReserveItem::getPhone, phone);
        queryWrapper.like(!ObjectUtils.isEmpty(reserveTime), ReserveItem::getReserveTime, reserveTime);


        IPage<ReserveItem> iP = reserveItemMapper.selectPage(iPage, queryWrapper);


        for (ReserveItem item : iP.getRecords()) {

            Reserve reserve = reserveMapper.selectOne(new LambdaQueryWrapper<Reserve>().eq(Reserve::getId, item.getReserveId()));
            item.setAvatar(reserve.getAvatar());
            item.setRemark(reserve.getRemark());
        }

        return R.success(iP.getRecords(), (Long) iP.getTotal());
    }
}
